package com.smile.frame.springbootsatoken.controller;


import cn.hutool.core.util.IdUtil;
import com.smile.frame.springbootsatoken.config.*;
import com.smile.frame.springbootsatoken.config.redis.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private RedisService redisService;

    // 测试登录，浏览器访问： http://localhost:8082/user/doLogin?username=admin&password=admin
    @RequestMapping("/doLogin")
    public Result<TokenInfo> doLogin(String username, String password) {
        TokenInfo tokenInfo = null;
        // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
        if ("admin".equals(username) && "admin".equals(password)) {
            Long userId = 10001L;
            tokenInfo = new TokenInfo();
            String token = IdUtil.fastSimpleUUID();
            String tokenKey = "tafb:auth:" + TokenConstants.LOGIN_TYPE_ADMIN + ":token:" + token;
            redisService.set(tokenKey, userId, 86400);
            String accountSessionKey = "tafb:auth:" + TokenConstants.LOGIN_TYPE_ADMIN + ":session:" + userId;
            AuthSession authSession = new AuthSession(accountSessionKey);
            authSession.setType(TokenConstants.SESSION_TYPE_ACCOUNT);
            authSession.setLoginType(TokenConstants.LOGIN_TYPE_ADMIN);
            authSession.setLoginId(userId);
            redisService.set(accountSessionKey, authSession, 86400);


            String tokenSessionKey = "tafb:auth:" + TokenConstants.LOGIN_TYPE_ADMIN + ":token-session:" + token;
            AuthSession authSession2 = new AuthSession(tokenSessionKey);
            authSession2.setType(TokenConstants.SESSION_TYPE_ACCOUNT);
            authSession2.setLoginType(TokenConstants.LOGIN_TYPE_ADMIN);
            authSession2.setToken(token);
            authSession2.set("userInfo", "[1,2,3,4,5,6,7,8,9,10]");
            redisService.set(tokenSessionKey, authSession2, 86400);

            tokenInfo.setLoginId(userId);
            tokenInfo.setLoginType(TokenConstants.LOGIN_TYPE_ADMIN);
            tokenInfo.setTokenName(TokenConstants.AUTHORIZATION_KEY);
            tokenInfo.setTokenPrefix("");
            tokenInfo.setTokenValue(token);
        }
        return Result.ok(tokenInfo);
    }

    // 浏览器访问： http://localhost:8082/user/get
    @Perm("get")
    @RequestMapping("/get")
    public Result<String> get() {
        HttpServletRequest request = SpringMVCUtil.getRequest();
        String platformType = request.getHeader(TokenConstants.PLATFORM_TYPE);
        String token = request.getHeader(TokenConstants.AUTHORIZATION_KEY);
        log.info("platformType:{},token:{}", platformType, token);
        return Result.ok();
    }

    @Perm("add")
    @RequestMapping("/add")
    public Result<Void> add() {
        return Result.ok();
    }

}
